<?php
    class FormulasDesarrolladasController extends AppController {
        var $name = 'FormulasDesarrolladas';
        var $uses = array('FormulaDesarrollada');
        var $helpers = array('Javascript');
        var $components = array('AdminParameters');

        function formulasDesarrolladas(){

            $this->render("formulas_desarrolladas", "ajax");
        }

        function  getList(){
            $this->render("list", "ajax");
        }

        function getListData($page=1, $limit=10, $orderColumn='fecha', $ascDesc='desc'){

            $result = array("data" => array(), "pageInfo" => array(
		    "total" => 0,
		    "page"  => 1,
		    "limit" => 1
		));



            $this->set("result", $result);

            $this->render("/elements/ajax_return", "ajax");

        }

        function getDetailData($id){
            $data = $this->FormulaDesarrollada->find("all", array("conditions" => array("FormulaDesarrollada.id" => $id), "recursive" => 0));

            /*"fields" => array("Alimento.id", "Alimento.alimento",
                            "Alimento.carbohidratos_total_g",  "Alimento.proteinas_g",
                            "Alimento.carbohidratos_disponible_g",
                            "Alimento.energia_kcal", "Alimento.grasa_total_g",
                            "Alimento.ajuste_g_cal", "FormulaAlimento.cantidad"),*/

            $al = $this->FormulaDesarrollada->FormulaAlimento->find(
                    "all",
                    array(
                            "conditions" => array(
                                "FormulaAlimento.formula_desarrollada_id" => $id
                            ),
                            "order" => array("FormulaAlimento.id ASC")
                        )
             );

            $data[0]["FormulaDesarrollada"]["alimentos"] = $al;

            $this->set("result", array("data" => $data));

            $this->render("/elements/ajax_return", "ajax");
        }

        function save(){

            $params = $this->params["form"];

            if($params["op"] == "add"){
                $this->__add($params);
            }
            if($params["op"] == "edit"){
                $this->__edit($params);
            }
            if($params["op"] == "del"){
                $this->__del($params);
            }

            $this->set("result", array("result" => true));


            $this->render("/elements/ajax_return", "ajax");
        }

        function __edit($params){
            $fa = $this->FormulaDesarrollada->FormulaAlimento;

            $idFF = $params["id"];

            $fa->deleteAll(array(
               "FormulaAlimento.formula_desarrollada_id" => $idFF
            ));

            $alimentos = json_decode($params["alimentos"]);

            if($alimentos != NULL){
                foreach($alimentos as $al){
                    $arrSave = array(
                      "formula_desarrollada_id" => $idFF,
                      "alimento_id" => $al->id,
                      "cantidad" => $al->cantidad
                    );

                    $fa->create($arrSave);
                    $fa->save();
                }
            }

            $this->FormulaDesarrollada->read(null, $idFF);

            $arrSave = array(
              "id" => $params["id"],
              "paciente_id" => $params["paciente_id"],
              "fecha" => $params["fecha"],
              "descripcion" => $params["descripcion"],
              "evaluacion_id" => $params["evaluacion_id"],
              "porcentaje_gr" => $params["porcentaje_gr"],
              "porcentaje_hc" => $params["porcentaje_hc"],
              "req_proteico" => $params["req_proteico"],
              "vet" => $params["vet"],
              "actual_ideal" => $params["actual_ideal"]
            );

            $this->FormulaDesarrollada->set($arrSave);
            $this->FormulaDesarrollada->save();

            return true;
        }

        function __add($params){
            $fa = $this->FormulaDesarrollada->FormulaAlimento;

            $arrSave = array(
              "paciente_id" => $params["paciente_id"],
              "fecha" => $params["fecha"],
              "descripcion" => $params["descripcion"],
              "evaluacion_id" => $params["evaluacion_id"],
              "porcentaje_gr" => $params["porcentaje_gr"],
              "porcentaje_hc" => $params["porcentaje_hc"],
              "req_proteico" => $params["req_proteico"],
              "vet" => $params["vet"],
              "actual_ideal" => $params["actual_ideal"]
            );

            $this->FormulaDesarrollada->create($arrSave);
            $this->FormulaDesarrollada->save();

            $idFF = $this->FormulaDesarrollada->getInsertID();

            $alimentos = json_decode($params["alimentos"]);

            if($alimentos != NULL){
                foreach($alimentos as $al){
                    $arrSave = array(
                      "formula_desarrollada_id" => $idFF,
                      "alimento_id" => $al->id,
                      "cantidad" => $al->cantidad
                    );

                    $fa->create($arrSave);
                    $fa->save();
                }
            }

            return true;

        }

        function __del($params){
            $fa = $this->FormulaDesarrollada->FormulaAlimento;

            $idFF = $params["id"];

            $fa->deleteAll(array(
               "FormulaAlimento.formula_desarrollada_id" => $idFF
            ));

            $this->FormulaDesarrollada->delete($idFF);
        }

        function getFormulasDesarrolladasPaciente($idPaciente){
            $data = $this->FormulaDesarrollada->find(
                    "list",
                    array(
                        "fields" => array("FormulaDesarrollada.id", "FormulaDesarrollada.fecha"),
                        "conditions" => array("FormulaDesarrollada.paciente_id" => $idPaciente),
                        "order" => array("FormulaDesarrollada.fecha DESC")
                        )
                    );

            $this->set("result", array("data" => $data));

            $this->render("/elements/ajax_return", "ajax");
        }

    }
?>